package com.uzai.console.service.pyq.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.pyq.hdkgoods.PyqHdkGoodsJobBatchSendDao;
import com.uzai.console.dto.pyq.hdkgoods.PyqHdkGoodsJobSendDao;
import com.uzai.console.dto.pyq.hdkgoods.PyqHdkGoodsQuery;
import com.uzai.console.dto.pyq.pyqgoods.PyqJobBatchSendDao;
import com.uzai.console.dto.pyq.pyqgoods.PyqJobSendDao;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.PyqDevicePlan;
import com.uzai.console.entity.PyqHdkGoods;
import com.uzai.console.entity.PyqMaterial;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.PyqDevicePlanMapper;
import com.uzai.console.mapper.PyqHdkGoodsMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.feign.UzaiCmsFeignService;
import com.uzai.console.service.pyq.*;
import com.uzai.console.vo.cms.tbgoods.DtkGoodsDetailVo;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.feign.uzaicms.SupperSearchVo;
import com.uzai.console.vo.feign.uzaicms.TypeSearchVo;
import com.uzai.console.vo.pyq.hdkgoods.PyqHdkGoodsVo;
import com.uzai.console.vo.pyq.pyqjob.PyqJobVo;
import com.uzai.console.vo.pyq.pyqmaterial.PyqMaterialVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * 朋友圈商品列表
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PyqHdkGoodsServiceImpl implements PyqHdkGoodsService {

    Logger logger = LoggerFactory.getLogger(PyqHdkGoodsServiceImpl.class);

    @Autowired
    private PyqDevicePlanMapper pyqDevicePlanMapper;
    @Autowired
    private PyqHdkGoodsSyncService pyqHdkGoodsSyncService;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private PyqHdkGoodsMapper pyqHdkGoodsMapper;
    @Autowired
    private PyqDevicePlanService pyqDevicePlanService;
    @Autowired
    private DeviceService deviceService;

    /**
     * 查询朋友圈好单库商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqHdkGoodsQuery
     * @return null
     */
    public Page<PyqHdkGoodsVo> findPyqHdkGoodsList(PyqHdkGoodsQuery pyqHdkGoodsQuery){
        //新建返回到页面的LIST对象
        List<PyqHdkGoodsVo> pyqHdkGoodsVoList = new ArrayList<>();
        //执行分页查询对象
        Page<PyqHdkGoods> page = new Page<>();
        pyqHdkGoodsQuery.convert(page);
        ArrayList<PyqHdkGoods> pyqHdkGoodsList =  pyqHdkGoodsMapper.findByList(page, pyqHdkGoodsQuery);
        if(pyqHdkGoodsList != null && pyqHdkGoodsList.size() > 0){
            for (PyqHdkGoods pyqHdkGoods : pyqHdkGoodsList){
                PyqHdkGoodsVo pyqHdkGoodsVo = new PyqHdkGoodsVo();
                BeanUtils.copyProperties(pyqHdkGoods, pyqHdkGoodsVo);
                pyqHdkGoodsVoList.add(pyqHdkGoodsVo);
            }
        }

        //返回到页面分页对象
        Page<PyqHdkGoodsVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(pyqHdkGoodsVoList);
        return pageVo;
    }

    /**
     * 批量发送朋友任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void batchSendPyqJpb(PyqHdkGoodsJobBatchSendDao pyqHdkGoodsJobBatchSendDao){
        if(pyqHdkGoodsJobBatchSendDao.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqHdkGoodsJobBatchSendDao.getDevicePlanId(), pyqHdkGoodsJobBatchSendDao.getMerId());
        if(pyqDevicePlan == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择的设备方案不存在");
        }

        //检验该设备方案是否能正常发送朋友圈
//        if(!pyqDevicePlanService.validatePyqDevicePlan(pyqDevicePlan)){
//            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案配置的机器人联盟和pid不能转链");
//        }


        if(pyqDevicePlan.getDeviceModel() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人模式");
        }

        //发送朋友圈的机器人列表
        List<Long> deviceUniqueIdList = null;
        if(pyqDevicePlan.getDeviceModel().intValue() == 0){ //发送部分机器人模式
            //验证发送机器人
            deviceUniqueIdList = JSONObject.parseArray(pyqDevicePlan.getDeviceInfo(), Long.class);
        }else if(pyqDevicePlan.getDeviceModel().intValue() ==1){ //全部机器人
            DeviceQuery deviceQuery = new DeviceQuery();
            deviceQuery.setMerId(pyqHdkGoodsJobBatchSendDao.getMerId());
            deviceQuery.setDeviceType(pyqDevicePlan.getDeviceType());
            List<DeviceInfoVo> deviceList = deviceService.findDeviceList(deviceQuery);
            if(deviceList != null && deviceList.size() > 0){
                deviceUniqueIdList = new ArrayList<>();
                for (DeviceInfoVo deviceInfoVo : deviceList){
                    deviceUniqueIdList.add(deviceInfoVo.getId());
                }
            }
        }

        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案未配置机器人");
        }

        pyqHdkGoodsJobBatchSendDao.setType(0); //默认商品
        pyqHdkGoodsJobBatchSendDao.setPlatform(0); //默认淘宝商品
        pyqHdkGoodsJobBatchSendDao.setDomain(pyqDevicePlan.getDomain());

        List<Long> idList = pyqHdkGoodsJobBatchSendDao.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择商品");
        }

        if(idList.size() > 1){ //批量发送，需要设置时间间隔
            if(pyqHdkGoodsJobBatchSendDao.getTimeInterval() == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入间隔时间");
            }

            if(pyqHdkGoodsJobBatchSendDao.getTimeInterval() < 1 || pyqHdkGoodsJobBatchSendDao.getTimeInterval() > 1440 ){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "间隔时间范围在1~1440分钟之内");
            }
        }

        //后台异步调用批量发送朋友圈接口
        pyqHdkGoodsSyncService.batchAddJobSync(pyqHdkGoodsJobBatchSendDao, deviceUniqueIdList, pyqDevicePlan);

        try{
            Thread.sleep(500);
        }catch (Exception e){

        }
    }

    /**
     * 单个商品发送朋友任务
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void sendPyqJpb(PyqHdkGoodsJobSendDao pyqHdkGoodsJobSendDao){
        if(pyqHdkGoodsJobSendDao.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqHdkGoodsJobSendDao.getDevicePlanId(), pyqHdkGoodsJobSendDao.getMerId());
        if(pyqDevicePlan == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择的设备方案不存在");
        }

        //检验该设备方案是否能正常发送朋友圈
//        if(!pyqDevicePlanService.validatePyqDevicePlan(pyqDevicePlan)){
//            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案配置的机器人联盟和pid不能转链");
//        }

        if(pyqDevicePlan.getDeviceModel() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择发送机器人模式");
        }

        //发送朋友圈的机器人列表
        List<Long> deviceUniqueIdList = null;
        if(pyqDevicePlan.getDeviceModel().intValue() == 0){ //发送部分机器人模式
            //验证发送机器人
            deviceUniqueIdList = JSONObject.parseArray(pyqDevicePlan.getDeviceInfo(), Long.class);
        }else if(pyqDevicePlan.getDeviceModel().intValue() ==1){ //全部机器人
            DeviceQuery deviceQuery = new DeviceQuery();
            deviceQuery.setMerId(pyqHdkGoodsJobSendDao.getMerId());
            deviceQuery.setDeviceType(pyqDevicePlan.getDeviceType());
            List<DeviceInfoVo> deviceList = deviceService.findDeviceList(deviceQuery);
            if(deviceList != null && deviceList.size() > 0){
                deviceUniqueIdList = new ArrayList<>();
                for (DeviceInfoVo deviceInfoVo : deviceList){
                    deviceUniqueIdList.add(deviceInfoVo.getId());
                }
            }
        }

        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "该设备方案未配置机器人");
        }

        pyqHdkGoodsJobSendDao.setType(0); //默认商品
        pyqHdkGoodsJobSendDao.setPlatform(0); //默认好单库
        pyqHdkGoodsJobSendDao.setDomain(pyqDevicePlan.getDomain());

        if(StringUtils.isBlank(pyqHdkGoodsJobSendDao.getItemid())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择商品");
        }

        List<String> imgList = pyqHdkGoodsJobSendDao.getImgList();

        if(imgList != null && imgList.size() > 9){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "图片数量不能大于9张");
        }

        //后台异步调用单个商品发送朋友圈接口
        pyqHdkGoodsSyncService.addJobSync(pyqHdkGoodsJobSendDao, deviceUniqueIdList, pyqDevicePlan);

        try{
            Thread.sleep(500);
        }catch (Exception e){

        }

    }
}
